package m.jclecai.com.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import m.jclecai.com.R;
import m.jclecai.com.adapter.RecyclerAdapter;
import m.jclecai.com.bean.ZqItem;
import m.jclecai.com.listener.ISelectPlayListener;
import m.jclecai.com.utils.StringUtils;
import m.jclecai.com.utils.Utils;

/**
 * 多串过关
 * A simple {@link Fragment} subclass.
 */
public class TZPOPDCFragment extends Fragment {
    private ISelectPlayListener listener;
    //选择彩票的总数
    private int itemSize = 0,minCount = 0;
    Map<String, String> map;
    private boolean isDG = false;

    TextView oldTxtView;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View  view = inflater.inflate(R.layout.recyler_hemai, container, false);
        if(map == null)
            map = new HashMap<>();
        String[] names = getResources().getStringArray(R.array.lottery_tz_pf_type_name_list);
        names = StringUtils.filterData(names,"串",itemSize,minCount,isDG);

        String[] types = getResources().getStringArray(R.array.lottery_tz_pf_type_list);
        types = StringUtils.filterData(types,"[*]",itemSize,minCount,isDG);
        //多串过关有胆的话不显示
        for (ZqItem zqItem : Utils.ZQ_ITEM_LIST) {
            if (zqItem.isDan_select()) {
                names = new String[]{};
                break;
            }
        }
/*        if(minCount > 0)
            names = new String[]{};*/

        RecyclerView recycler_view = (RecyclerView) view.findViewById(R.id.recycler_view);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),3);
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        recycler_view.setLayoutManager(gridLayoutManager);
        final RecyclerAdapter recyclerAdapter = new RecyclerAdapter(getActivity(), names, types,map);
        recycler_view.setAdapter(recyclerAdapter);

        recyclerAdapter.setOnItemClickListener(new RecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(TextView v, int position) {
                if(recyclerAdapter.getFirstCheckView() != null)
                    recyclerAdapter.getFirstCheckView().setBackgroundResource(R.drawable.hemai_popuwindow_txt_bg);
                String key = v.getText().toString();
                String tag = v.getTag().toString();
                if(map.containsKey(key)){
                    map.remove(key);
                    v.setBackgroundResource(R.drawable.hemai_popuwindow_txt_bg);
                }else{
                    if(oldTxtView != null)oldTxtView.setBackgroundResource(R.drawable.hemai_popuwindow_txt_bg);
                    map.clear();
                    map.put(key, tag);
                    v.setBackgroundResource(R.drawable.hemai_popuwindow_txt_bg_press);
                    oldTxtView = v;
                }
            }
        });
        return view;
    }

    public String getData(){
        String d = "";
        int i = 0;
        for (Map.Entry<String, String> en : map.entrySet()){
            d+=en.getValue();
            if(i < map.size() -1)
                d+=",";
            i++;
        }
        return d;
    }

    public void setDG(boolean isDG){
        this.isDG = isDG;
    }

    public void setItemCount(int count){
        this.itemSize = count;
    }

    public void setListener(ISelectPlayListener listener) {
        this.listener = listener;
    }

    boolean check = false;
    public void setDefCheck(boolean check,String value){
        this.check=check;
        if(check) {
            if(map == null)
                map = new HashMap<>();
            String[] split = value.split(",");
            for (int i = 0; i < split.length; i++) {
                map.put(split[i].replaceAll("[*]", "串"), split[i]);
            }
        }
    }

    public void setMinCount(int minCount) {
        this.minCount = minCount;
    }

}
